package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.domain.model.User;
import com.moyuer.bsf.domain.service.UserService;
import com.moyuer.bsf.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tech.yunland.shop.domain.model.Goods;
import tech.yunland.shop.domain.model.GoodsPaid;
import tech.yunland.shop.domain.service.GoodsPaidService;
import tech.yunland.shop.domain.until.ChangeSerizliableResult;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预购商品信息控制器
 *
 * @author 王睿智 <geekrize@gmail.com>
 */
@RestController
@RequestMapping("/goods_paid")
public class GoodsPaidController extends BaseController {

	@Autowired
	private GoodsPaidService goodsPaidService;
	@Autowired
    private UserService userService;
	/**
	 * 查询会员预购商品
	 *
	 * @param userId
	 *            会员ID
	 * @param page
	 *            起始索引
	 * @param rows
	 *            每页数量
	 * @Return List<GoodsPaid>
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public Pagination<GoodsPaid> getOnShelfGoods(
			@RequestParam(required = false) String userId,
			@RequestParam(required = false) List<String> goodsId, 
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows) {

		Pagination<GoodsPaid> result = new Pagination<GoodsPaid>(page, rows);
		int startIdx = result.getStartIdx();

		int count = goodsPaidService.getResultSize(goodsId, userId, organIds);
		List<GoodsPaid> records = goodsPaidService.getResultInfos(goodsId, userId, startIdx, rows, organIds);

		result.setCount(count);
		result.setRecords(records);

		return result;
	}

	/**
	 * 查询店铺会员商品平均价格
	 * 
	 * @param goodsId
	 * @return
	 */
	@RequestMapping(value = "/all_member_prize", method = RequestMethod.GET)
	public List<Map<String, Object>> getAllMemberPrize(@RequestParam(required = false) String goodsId) {

		return goodsPaidService.getMemberPrize(primaryOrganId, goodsId);
	}

	/**
	 * 查询店铺会员剩余预购商品
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reserved_goods", method = RequestMethod.GET)
	public Pagination<HashMap<String, Object>> getReservedGoods(
			@RequestParam(required = false) int type,
			@RequestParam(required = false) String memberId, 
			@RequestParam(required = false) Integer page, 
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) String param) {
		
		Pagination<HashMap<String, Object>> result = new Pagination(page, rows);

		List<Map<String, Object>> reserved = new ArrayList<>();
		
		Integer startIdx = result.getStartIdx();
		Integer count = 0;
		if (type == 10) {// 按店铺查询剩余总额
			reserved = goodsPaidService.getReservedGoods(primaryOrganId);
			count=1;
		} else if (type == 20) {// 按会员查询剩余总额
			if(param==null){
				reserved = goodsPaidService.getMemberReserved(primaryOrganId,startIdx,rows,"details");
				count=goodsPaidService.getMemberReserved(primaryOrganId,startIdx,rows,"size").size();
			}else{
				//按会员名称模糊查询剩余总额
				reserved = goodsPaidService.getMemberReservedLike(primaryOrganId,param,startIdx,rows,"details");
				count=goodsPaidService.getMemberReservedLike(primaryOrganId,param,startIdx,rows,"size").size();
			}
		}else if (type == 30) {// 按会员查询明细
			reserved = goodsPaidService.getReservedEntry(primaryOrganId, memberId, startIdx, rows,"details");
			count=goodsPaidService.getReservedEntry(primaryOrganId, memberId, startIdx, rows,"size").size();
		}
		List<HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(reserved);
		result.setRecords(entyrs);
		result.setCount(count);
		return result;
	}
	/**
	 * 查询指定已购的商品
	 */
	@RequestMapping(value="/entry/{id}" ,method = RequestMethod.GET)
	//public Goods getGood(@PathVariable String id){
	public GoodsPaid getGoodsPaid(@PathVariable String id){
		
		GoodsPaid goods= goodsPaidService.getGoodsPaid(id);
		System.out.println(goods.getGoodsName());
		System.out.println(goods.getId());
		 return goods;
	}
		
		
	
	

	/**
	 * 查询热门商品购买力
	 * @param dateType    时间类型
	 * @param bizDate  日期
	 * @param page  起始索引
	 * @param rows    每页数量
	 * @return
	 */
	@RequestMapping(value="/topInfo" ,method = RequestMethod.GET)
	public Pagination<HashMap<String, Object>>getTopInfo(
			@RequestParam String dateType,
			@RequestParam String bizDate,
			@RequestParam Integer page,
			@RequestParam Integer rows){
		
			Pagination<HashMap<String, Object>> result = new Pagination(page, rows);
			
			List<Map<String, Object>> returnEntry = new ArrayList<>();
			Integer startIdx = result.getStartIdx();
			Integer count = 0;
			
			returnEntry = goodsPaidService.getTopInfo(dateType, bizDate, primaryOrganId, startIdx, rows, "details");
			count = goodsPaidService.getTopInfo(dateType, bizDate, primaryOrganId, startIdx, rows, "size").size();
			
			List <HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(returnEntry);
			result.setRecords(entyrs);
			result.setCount(count);

			return result;
	}
	/**
	 *查询顾客消费信息 
	 * @param dateType 时间类型
	 * @param bizDate 时间
	 * @param primaryOrganId 机构
	 * @return
	 */
	@RequestMapping(value="/shopInfo",method=RequestMethod.GET)
	public  Pagination<HashMap<String, Object>> getShopInfo(  
			@RequestParam String dateType,
			@RequestParam String bizDate,
			@RequestParam Integer page,
			@RequestParam Integer rows){
		Pagination<HashMap<String, Object>> result = new Pagination(page, rows);
		
		List<Map<String, Object>> returnEntry = new ArrayList<>();
		Integer startIdx = result.getStartIdx();
		Integer count = 0;
		
		returnEntry=goodsPaidService.getShopInfo(dateType, bizDate, primaryOrganId,startIdx,rows,"details");
		count = goodsPaidService.getShopInfo(dateType, bizDate, primaryOrganId, startIdx, rows, "size").size();
		
		List <HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(returnEntry);
		
		result.setRecords(entyrs);
		result.setCount(count);

		return result;
	}
	
	/**
	 * 查询会员消费能力
	 * @param dateType
	 * @param bizDate
	 * @param memberId
	 * @param page
	 * @param rows
	 * @returnHashMap<String, Object>
	 */
	@RequestMapping(value="/memberShopInfo",method=RequestMethod.GET)
	public Pagination<HashMap<String, Object>> getMemberShopInfo(
			@RequestParam String dateType,
			@RequestParam String bizDate,
			@RequestParam String memberId, 
			@RequestParam Integer page,
			@RequestParam Integer rows){
		Pagination<HashMap<String, Object>> result = new Pagination(page, rows);
		
		List<Map<String, Object>> returnEntry = new ArrayList<>();
		
		Integer startIdx = result.getStartIdx();
		Integer count = 0;
		returnEntry=goodsPaidService.getMemberShopInfo(dateType,bizDate, memberId, startIdx,rows,"details");
		count = goodsPaidService.getMemberShopInfo(dateType,bizDate, memberId, startIdx,rows,"size").size();
	
		List <HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(returnEntry);
		result.setRecords(entyrs);
		result.setCount(count);

		return result;
	}

	/**
	 * 查询顾客消费活跃度
	 * @param dateType
	 * @param bizDate
	 * @param memberId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/shopTimes",method=RequestMethod.GET)
	public  Pagination<HashMap<String, Object>> getShopTimes(
			@RequestParam String dateType,
			@RequestParam String bizDate,
			@RequestParam String memberId, 
			@RequestParam Integer page,
			@RequestParam Integer rows){
		Pagination<HashMap<String, Object>> result = new Pagination(page, rows);
		
		List<Map<String, Object>> returnEntry = new ArrayList<>();
		
		Integer startIdx = result.getStartIdx();
		Integer count = 0;
		returnEntry=goodsPaidService.getShopTimes(primaryOrganId,dateType, bizDate, memberId, startIdx, rows, "details");
		count = goodsPaidService.getShopTimes(primaryOrganId,dateType, bizDate, memberId, startIdx, rows, "size").size();
		
		List <HashMap<String,Object>> entyrs = ChangeSerizliableResult.changeToSerizliable(returnEntry);
		result.setRecords(entyrs);
		result.setCount(count);

		return result;
	}
	
	/**
	 * 会员退卡
	 * @param goodsId
	 * @param money
	 * @param member_id
	 * @param quantity
	 */
	@RequestMapping(value="/updateMemberCard",method=RequestMethod.POST)
	public void updateMemberCard(
			@RequestParam (required = false)String memberId,
			@RequestParam(required = false) BigDecimal money,
			@RequestParam String goodsPaidId,
			@RequestParam String goodsId,
			@RequestParam BigDecimal number){
		User member = userService.get(memberId);
		String name = member.getName();
		goodsPaidService.updateMemberCard(memberId, money,primaryOrganId, goodsPaidId,goodsId,number);
		// 保存日志
		saveLog("会员:[" + name + "]退卡["+number +"次]成功！");
	}
}








